package org.dromara.assets.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ConfigReader {
    // 存储解析后的配置信息
    private static Map<String, Properties> cf = null;

    public static String getConfig(String selection, String key) {
        if (cf == null) {
            cf = new HashMap<>();
            try (FileInputStream fis = new FileInputStream("/etc/config/python.ini")) {
                java.util.Scanner scanner = new java.util.Scanner(fis);
                Properties currentSection = null;
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine().trim();
                    if (line.startsWith("[") && line.endsWith("]")) {
                        // 解析节名
                        String section = line.substring(1, line.length() - 1);
                        currentSection = new Properties();
                        cf.put(section, currentSection);
                    } else if (currentSection != null && line.contains("=")) {
                        // 解析键值对
                        String[] parts = line.split("=", 2);
                        String k = parts[0].trim();
                        String v = parts[1].trim();
                        currentSection.setProperty(k, v);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 从对应节中获取键的值
        Properties sectionProps = cf.get(selection);
        if (sectionProps != null) {
            return sectionProps.getProperty(key);
        }
        return null;
    }
}